#!/bin/bash

Install_PHP()
{

    echo -e "\033[34m[+] Installing ${PHP_Ver}...\033[0m"

    cd ${cur_dir}/src
    tar jxf ${PHP_Ver}.tar.bz2
    cd ${PHP_Ver}
    ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/conf.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-intl --enable-pcntl --enable-ftp --with-gd --with-openssl --with-mhash --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --enable-opcache --with-xsl
    make ZEND_EXTRA_LIBS='-liconv' -j `grep 'processor' /proc/cpuinfo | wc -l`
    if [ $? -ne 0 ]; then
        make ZEND_EXTRA_LIBS='-liconv'
    fi
    make install

    ln -sf /usr/local/php/bin/php /usr/bin/php
    ln -sf /usr/local/php/bin/phpize /usr/bin/phpize
    ln -sf /usr/local/php/bin/pear /usr/bin/pear
    ln -sf /usr/local/php/bin/pecl /usr/bin/pecl
    ln -sf /usr/local/php/sbin/php-fpm /usr/bin/php-fpm
    rm -f /usr/local/php/conf.d/*

    echo "Copy new php configure file..."
    mkdir -p /usr/local/php/{etc,conf.d}
    \cp php.ini-production /usr/local/php/etc/php.ini
    echo "Modify php.ini......"
    sed -i 's/post_max_size = 8M/post_max_size = 2048M/g' /usr/local/php/etc/php.ini
    sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 2048M/g' /usr/local/php/etc/php.ini
    sed -i 's/;date.timezone =/date.timezone = PRC/g' /usr/local/php/etc/php.ini
    sed -i 's/short_open_tag = Off/short_open_tag = On/g' /usr/local/php/etc/php.ini
    sed -i 's/; cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
    sed -i 's/;cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
    sed -i 's/max_execution_time = 30/max_execution_time = 300/g' /usr/local/php/etc/php.ini
    sed -i 's/max_input_time = 60/max_input_time = 600/g' /usr/local/php/etc/php.ini
    sed -i 's/default_socket_timeout = 60/default_socket_timeout = 600/g' /usr/local/php/etc/php.ini
    sed -i 's/memory_limit = 128M/memory_limit = 4096M/g' /usr/local/php/etc/php.ini
    sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server/g' /usr/local/php/etc/php.ini
    echo "Write Opcache to php.ini......"
    cat >>/usr/local/php/etc/php.ini<<EOF
[Zend Opcache]
zend_extension=opcache.so
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
EOF
    pear config-set php_ini /usr/local/php/etc/php.ini
    pecl config-set php_ini /usr/local/php/etc/php.ini
    curl -sS --connect-timeout 30 -m 60 https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
    if [ $? -eq 0 ]; then
        echo "Composer install successfully."
    else
        if [ -s /usr/local/php/bin/php ]; then
            wget --prefer-family=IPv4 --no-check-certificate -T 120 -t3 ${Download_Mirror}/composer/composer.phar -O /usr/local/bin/composer
            if [ $? -eq 0 ]; then
                echo "Composer install successfully."
            else
                echo "Composer install failed!"
            fi
            chmod +x /usr/local/bin/composer
        fi
    fi
    echo "Copy Opcache Control Panel..."
    mkdir -p ${Default_Moodle_Dir}/moodle/
    \cp ${cur_dir}/conf/ocp.php ${Default_Moodle_Dir}/moodle/ocp.php
    \cp ${cur_dir}/conf/p.php ${Default_Moodle_Dir}/moodle/p.php
    echo "<?php phpinfo(); ?>" >> ${Default_Moodle_Dir}/moodle/info.php
    echo "Creating new php-fpm configure file..."
    cat >/usr/local/php/etc/php-fpm.conf<<EOF
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
log_level = error
[www]
listen = /run/php-cgi.sock
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
request_terminate_timeout = 600
request_slowlog_timeout = 0
slowlog = var/log/slow.log
EOF
    echo "Copy php-fpm init.d file..."
    \cp ${cur_dir}/src/${PHP_Ver}/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    chmod +x /etc/init.d/php-fpm
    rm -fr ${cur_dir}/src/${PHP_Ver}
}
